<?php

require_once $_SERVER['srcPath'].'/GeneralServicesController.php';
require_once $_SERVER['srcPath'].'/Db.php';
require_once 'TestUtils.php';


use GrouppaGroup\GeneralServicesController;
use GrouppaGroup\MessageFromApp;
use GrouppaGroup\ApplicationTCPConnection;
use GrouppaGroup\Db;

class GeneralServicesControllerTest extends PHPUnit_Framework_TestCase
{

	/**
	 * @var GeneralServicesController 
	 */
	public $instance;
	private $lastUsersIndex;
	public function setUp()
	{
		$this->instance = new GeneralServicesController();
		$this->lastUsersIndex = TestUtils::getAutoIncrementValue('users');
	}
	
	public function tearDown()
	{
		//delete all created accounts
		Db::getInstance()->sendQuery("DELETE FROM users WHERE id_user >= {$this->lastUsersIndex}");
	}

	public function userNamePasswordGenerator()
	{
		return array(
				array("test1","mongo123","blabla@dfasdf.net"),
				array("test2","dsfas","airplane@tokyo.flying"),
				array("test3","faeradx3424","maniek@atakuje.vdfv"),
				array('test4','vsdfseewr',"megazord@attack.sad"),
				array('test5','VweDv9q3',"sylwekwazuuup@gmail.com")
		);
	}
	
	
	public function userNamePasswordGenerator2()
	{
		return array(
				array("test5","mongo123","blabla@dfasdf.net"),
				array("test6","dsfas","airplane@tokyo.flying"),
				array("test7","faeradx3424","maniek@atakuje"),
				array('test8','vsdfseewr',"megazord@attack")
		);
	}
	
	/**
	 * test for account creating
	 * @param string $userName
	 * @param string $password
	 * @dataProvider userNamePasswordGenerator
	 */
	public function testCreateAccount($userName,$password,$email)
	{
		echo "\n\nTesting create account\n";
		$message = TestUtils::createMessageFromApp($userName,1,
			json_decode('{"username":"'.$userName.'","password":"'.$password.'","email":"'.$email.'"}'),'createAccount','GeneralServices');
		
		$result = $this->instance->createAccount($message);
		if(!$result)
		{
			echo "Test failed: last message params: ".TestUtils::getLastMessageParams()."\n";
		}
		$this->assertTrue($result);
		
		$this->loginTest($email, $password);
		
		//$this->resetPasswordTest($userName, $password, $email);
	}
	
	
	/**
	 * @param array $params accounts created by testCreateAccount
	 */
	public function loginTest($email, $password)
	{
		echo "\n\nTesting login\n";
		

		$message = TestUtils::createMessageFromApp("",1,
				json_decode('{"email":"'.$email.'","password":"'.$password.'"}'),'login','GeneralServices');
		
		$result = $this->instance->login($message);

		if(!$result)
		{
			echo "Test failed: last message params: ".TestUtils::getLastMessageParams()."\n";
		}
		$this->assertTrue($result);
		
		$this->loginTest2($email);
	}
	
	public function loginTest2($email)
	{
		echo "\n\nTesting login with false password\n";
		
		$password = "sdxeazvv";

		$message = TestUtils::createMessageFromApp("",1,
				json_decode('{"email":"'.$email.'","password":"'.$password.'"}'),'login','GeneralServices');
		
		$result = $this->instance->login($message);

		$this->assertFalse($result);
		
	}
	
	public function resetPasswordTest($userName,$password,$email)
	{
		echo "\n\nTesting reset password\n";
		$message = TestUtils::createMessageFromApp($userName,1,
			json_decode('{"email":"'.$email.'"}'),'resetPassword','GeneralServices');
		
		$result = $this->instance->resetPassword($message);
		
		if(!$result)
		{
			echo "Test failed: last message params: ".TestUtils::getLastMessageParams()."\n";
		}
		$this->assertTrue($result);
		
		
	}
	
	/**
	 * @dataProvider userNamePasswordGenerator2
	 */
	public function testLogin3($userName,$password,$email)
	{
		echo "\n\nTesting login with false username and password\n";
		

		$message = TestUtils::createMessageFromApp($userName,1,
				json_decode('{"email":"'.$email.'","password":"'.$password.'"}'),'login','GeneralServices');
		
		$result = $this->instance->login($message);

		$this->assertFalse($result);
		
	}
	
}